/**
 *
 */
package com.jeecms.wicool.manager.report.impl;

import com.google.common.collect.Lists;
import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.common.page.Pagination;
import com.jeecms.common.util.DateUtils;
import com.jeecms.wicool.dao.report.ReportDao;
import com.jeecms.wicool.domain.PeiSongDianDomain;
import com.jeecms.wicool.domain.SaleCountReportDomain;
import com.jeecms.wicool.domain.SaleDayReportDomian;
import com.jeecms.wicool.domain.SaleMoneyReportDomain;
import com.jeecms.wicool.entity.orders.OrderProductMapping;
import com.jeecms.wicool.entity.orders.Orders;
import com.jeecms.wicool.entity.product.ProductSku;
import com.jeecms.wicool.entity.util.Dictionary2;
import com.jeecms.wicool.manager.orders.OrderProductMappingMng;
import com.jeecms.wicool.manager.orders.OrdersMng;
import com.jeecms.wicool.manager.product.ProductSkuMng;
import com.jeecms.wicool.manager.report.ReportMng;
import com.jeecms.wicool.manager.util.DictionaryMng;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author spring
 */
@Service
@Transactional
public class ReportMngImpl implements ReportMng {
    @Autowired
    private ReportDao reportDao;

    @Autowired
    private OrdersMng ordersMng;

    @Autowired
    private OrderProductMappingMng orderProductMappingMng;

    @Autowired
    private ProductSkuMng productSkuMng;

    @Autowired
    private DictionaryMng dictionaryMng;

    public List<Map<String, Object>> getSaleDayReportPage(String saleDate, CmsUser user) {
        return reportDao.getSaleDayReportPage(saleDate, user);
    }

    public Map<String, Object> getSaleDayReportPayment(String saleDate, CmsUser user) {
        return reportDao.getSaleDayReportPayment(saleDate, user);
    }

    public List<Map<String, Object>> getSaleMonthReportPage(String beginDate, String endDate, CmsUser user) {
        return reportDao.getSaleMonthReportPage(beginDate, endDate, user);
    }

    public Map<String, Object> getSaleMonthReportPayment(String beginDate, String endDate, CmsUser user) {
        return reportDao.getSaleMonthReportPayment(beginDate, endDate, user);
    }

    public  List<Map<String,Object>> getUserScoreList(String userName ,String mobile,String endTime,String zhuanUserName,String zhuanMobile)
    {
    	 return reportDao.getUserScoreList(userName, mobile, endTime, zhuanUserName, zhuanMobile);
    }
    
    @Override
    public List<SaleCountReportDomain> getDisSaleCount(Short[] status, Integer disAddId, Date startTime, Date endTime, Integer typeId) {
        List<Orders> ordersList = ordersMng.list(status, disAddId, startTime, endTime, typeId);
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        List<SaleCountReportDomain> result = Lists.newLinkedList();
        for (Orders orders : ordersList) {
            List<OrderProductMapping> mappings = orderProductMappingMng.getByOrderId(orders.getId());
            for (OrderProductMapping mapping : mappings) {
                int buyCount = mapping.getBuyCount();
                Integer totalCount = map.get(mapping.getProductSku().getId());
                if (totalCount == null) {
                    totalCount = 0;
                }
                totalCount += totalCount + buyCount;
                map.put(mapping.getProductSku().getId(), totalCount);
            }
        }
        int counter = 0;
        for (Integer productSkuId : map.keySet()) {
            ProductSku productSku = productSkuMng.findById(productSkuId);
            SaleCountReportDomain domain = new SaleCountReportDomain(counter++, productSku.getProduct().getProductName(), productSku.getSkuName(), map.get(productSkuId));
            result.add(domain);
        }
        return result;
    }

    @Override
    public Pagination getDisSalePrice(Short[] status, Integer disAddId, Date startTimeDate, Date endTimeDate, Integer typeId, Integer pageNo, Integer pageSize) {
        Pagination pagination = ordersMng.list(status, disAddId, startTimeDate, endTimeDate, typeId, pageNo, pageSize);
        List<Orders> ordersList = (List<Orders>) pagination.getList();
        List<SaleMoneyReportDomain> result = Lists.newLinkedList();
        int counter = 0;
        List<Dictionary2> pay_type = dictionaryMng.getList("pay_type");
        List<Dictionary2> activity_type = dictionaryMng.getList("activity_type");
        Map<Integer, String> payWay = new HashMap<Integer, String>();
        Map<Integer, String> activeWay = new HashMap<Integer, String>();
        for (Dictionary2 dictionary2 : pay_type) {
            payWay.put(dictionary2.getDicCode().intValue(), dictionary2.getDicName());
        }
        for (Dictionary2 dictionary2 : activity_type) {
            activeWay.put(dictionary2.getDicCode().intValue(), dictionary2.getDicName());
        }


        for (Orders orders : ordersList) {
        	counter++;
            SaleMoneyReportDomain domain = new SaleMoneyReportDomain(counter, orders.getTotalPrice(), orders.getDiscountPrice(), orders.getOrderPrice(),orders.getCouponPrice(), payWay.get(orders.getPayway().intValue()), activeWay.get(orders.getTypeId()), orders.getBackPrice());
            result.add(domain);
        }
        pagination.setList(result);

        return pagination;
    }

    


    @Override
    public List<SaleMoneyReportDomain> getDisSalePrice(Short[] status, Integer disAddId, Date startTimeDate, Date endTimeDate, Integer typeId) {
        List<Orders> ordersList = ordersMng.list(status, disAddId, startTimeDate, endTimeDate, typeId);
        List<SaleMoneyReportDomain> result = Lists.newLinkedList();
        int counter = 0;
        List<Dictionary2> pay_type = dictionaryMng.getList("pay_type");
        List<Dictionary2> activity_type = dictionaryMng.getList("activity_type");
        Map<Integer, String> payWay = new HashMap<Integer, String>();
        Map<Integer, String> activeWay = new HashMap<Integer, String>();
        for (Dictionary2 dictionary2 : pay_type) {
            payWay.put(dictionary2.getDicCode().intValue(), dictionary2.getDicName());
        }
        for (Dictionary2 dictionary2 : activity_type) {
            activeWay.put(dictionary2.getDicCode().intValue(), dictionary2.getDicName());
        }


        for (Orders orders : ordersList) {
        	counter++;
            SaleMoneyReportDomain domain = new SaleMoneyReportDomain(counter, orders.getTotalPrice(), orders.getDiscountPrice(), orders.getOrderPrice(),orders.getCouponPrice(), payWay.get(orders.getPayway().intValue()), activeWay.get(orders.getTypeId()), orders.getBackPrice());
            result.add(domain);
        }

        return result;
    }
    
    public List<PeiSongDianDomain> getDisSalePrice(Integer disAddId, String startTimeDate, String endTimeDate, Integer typeId){
    	  List<Map<String,Object>> list = reportDao.getDisSalePrice(disAddId, startTimeDate, endTimeDate, typeId);
    	  List<PeiSongDianDomain> result = Lists.newLinkedList();
    	  
    	  if(list != null && !list.isEmpty()){
    		  for (Map<String,Object> map : list) {
    			  PeiSongDianDomain domain = new PeiSongDianDomain(null2Empty(map.get("num")), null2Empty(map.get("address")), null2Empty(map.get("product_name")), null2Empty(map.get("sku_name")), null2Empty(map.get("product_price")), null2Empty(map.get("sale_count")), null2Empty(map.get("sale_money_count")));
    			  result.add(domain);
			  }
    	  }
    	  return result;
    }
    
    public String null2Empty(Object obj){
		return obj == null ? null : obj.toString();
	}
    
    public List<SaleDayReportDomian> getSaleDayPrice(String startTimeDate, String endTimeDate, Integer typeId){
    	 List<Orders> ordersList = ordersMng.list(startTimeDate, endTimeDate, typeId);
    	 List<SaleDayReportDomian> result = Lists.newLinkedList();
         List<Dictionary2> activity_type = dictionaryMng.getList("activity_type");
         List<Dictionary2> pay_type = dictionaryMng.getList("pay_type");
         Map<Integer, String> payWay = new HashMap<Integer, String>();
         Map<Integer, String> activeWay = new HashMap<Integer, String>();
         for (Dictionary2 dictionary2 : pay_type) {
             payWay.put(dictionary2.getDicCode().intValue(), dictionary2.getDicName());
         }
         for (Dictionary2 dictionary2 : activity_type) {
             activeWay.put(dictionary2.getDicCode().intValue(), dictionary2.getDicName());
         }
         
         for (Orders orders : ordersList) {
         	SaleDayReportDomian domain = new SaleDayReportDomian();
         	domain.setXingming(orders.getConsigneeName());
         	domain.setMobile(orders.getMobile()+"");
            domain.setAddress(orders.getConsigneeAddr());
            domain.setPeisongtime(orders.getDistributionTime());
            domain.setOrdercode(orders.getOrderCode());
            domain.setOrderdate(orders.getOrderDate() == null ? null : DateUtils.format2.format(orders.getOrderDate()));
         	
            String productname = "";
            Set<OrderProductMapping> mapping = orders.getMappingSet();
         	for (OrderProductMapping orderProductMapping : mapping) {
         		productname += orderProductMapping.getProduct() == null ? "" : orderProductMapping.getProduct().getProductName() ;
         		productname += ";";
         	}
         	domain.setProductname(productname);;
         	
         	if(orders.getType() == 4)
         	{
         		domain.setReservationflag("是");
         	}else{
         		domain.setReservationflag("否");
         	}
         	
         	domain.setPayway(payWay.get(orders.getPayway().intValue()));
         	domain.setTotalmoney(orders.getOrderPrice()+"");
         	domain.setPeisongfee("0");
//         	if(orders.getCouponPrice() != null && orders.getCouponPrice().compareTo(BigDecimal.ZERO) == 1){
//         		domain.setDiscountflag("是");
//         	}else{
         		domain.setDiscountflag("否");
//         	}
         	domain.setDiscountmoney(orders.getOrderPrice()+"");
            result.add(domain);
         }

         return result;
    }
}
